package com.rongke.web.quartz;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.model.*;
import com.rongke.service.*;
import com.rongke.sms.SmsService;
import com.rongke.sms.SmsTempletEnum;
import com.rongke.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/7 0007.
 */
@Component("TimeJob")
@Transactional
@CrossOrigin
public class TimeJob {
    @Autowired
    private UserService userService;
    @Autowired
    private LoanOrderService loanOrderService;
    @Autowired
    private ParamSettingService paramSettingService;
    @Autowired
    private MsgModelService msgModelService;
    @Autowired
    private SmsService smsService;


    public void printMessage() throws ParseException {
        //找到所以要处理的订单
        EntityWrapper<LoanOrder> ewOrder = new EntityWrapper();
        List<Integer> list = new ArrayList<Integer>(){{add(3);add(4);add(5);}};
        ewOrder.in("order_status",list);
        List<LoanOrder> loanOrderList = loanOrderService.selectList(ewOrder);
        if (loanOrderList.size()!=0){
            for(LoanOrder o :loanOrderList){
                //利率
                EntityWrapper<ParamSetting> ewParam= new EntityWrapper();
                ewParam.eq("id",o.getParamSettingId());
                ewParam.eq("status",1);
                ParamSetting paramSetting = paramSettingService.selectOne(ewParam);
                //状态为待还款时
                if(o.getOrderStatus()==3){
                    //判断是否到容限期（即逾期）
                    if(o.getLimitPayTime().getTime()< DateUtils.YYMMDDDate(new Date()).getTime()){
                        o.setOrderStatus(4);
                        //容限期（即逾期）一天费用
                        BigDecimal money = o.getBorrowMoney().multiply(new BigDecimal(paramSetting.getAllowPercent()*0.01)).setScale(1, BigDecimal.ROUND_HALF_UP);
                        o.setAllowMoney(money);
                        o.setAllowDays(1);
                        o.setOverdueDays(1);
                        o.setOverdueMoney(money);
                        o.setWateMoney(o.getWateMoney().add(money));
                        o.setNeedPayMoney(o.getNeedPayMoney().add(money));
                        loanOrderService.updateById(o);
                        //发送逾期短信
                        EntityWrapper<MsgModel> ewMsg = new EntityWrapper();
                        ewMsg.eq("type",5);
                        MsgModel msgModel = msgModelService.selectOne(ewMsg);//找到短信模板
                        //将信息部分替换
                        User user = userService.selectById(o.getUserId());
                        String content = msgModel.getContent().replaceAll("</?[^>]+>", "").replace("#userName",user.getUserName()).replace("#borrowMoney",o.getBorrowMoney().toString()).replace("overDays",o.getOverdueDays().toString()).replace("#allowMoney",o.getAllowMoney().toString()).replace("#needPayMoney",o.getNeedPayMoney().setScale(2, RoundingMode.HALF_UP).toString());
                        SmsTempletEnum.REMINDSEND.setTypeName(content);
                        smsService.smsCodeByType(user.getPhone(), 3);
                    }
                    //判断是否为还款前一天
                    else if(o.getLimitPayTime().getTime() == DateUtils.dayAdd(1,DateUtils.YYMMDDDate(new Date())).getTime()){
                        //发送短信
                        EntityWrapper<MsgModel> ewMsg = new EntityWrapper();
                        ewMsg.eq("type",2);
                        MsgModel msgModel = msgModelService.selectOne(ewMsg);//找到短信模板
                        //将信息部分替换
                        User user = userService.selectById(o.getUserId());
                        String content = msgModel.getContent().replaceAll("</?[^>]+>", "").replace("#userName",user.getUserName()).replace("#time",DateUtils.YYMMDDDateChinese(o.getGiveTime())).replace("#money",o.getNeedPayMoney().setScale(1, BigDecimal.ROUND_HALF_UP).toString());
                        SmsTempletEnum.REMINDSEND.setTypeName(content);
                        smsService.smsCodeByType(user.getPhone(), 3);
                    }else if(o.getLimitPayTime().getTime() == DateUtils.YYMMDDDate(new Date()).getTime()){
                        //发送短信
                        EntityWrapper<MsgModel> ewMsg = new EntityWrapper();
                        ewMsg.eq("type",3);
                        MsgModel msgModel = msgModelService.selectOne(ewMsg);//找到短信模板
                        //将信息部分替换
                        User user = userService.selectById(o.getUserId());
                        String content = msgModel.getContent().replaceAll("</?[^>]+>", "").replace("userName",user.getUserName()).replace("#money",o.getNeedPayMoney().toString());
                        SmsTempletEnum.REMINDSEND.setTypeName(content);
                        smsService.smsCodeByType(user.getPhone(), 3);
                    }
                }
                //状态为容限期的时候
                else if(o.getOrderStatus()==4){
                    //判断是否超出容限期
                    if(o.getOverdueTime().getTime()< DateUtils.YYMMDDDate(new Date()).getTime()){
                        o.setOrderStatus(5);
                        //超出容限期一天的费用
                        BigDecimal money = o.getBorrowMoney().multiply(new BigDecimal(paramSetting.getOverduePercent()*0.01)).setScale(1, BigDecimal.ROUND_HALF_UP);
                        o.setOverdueDays(o.getOverdueDays()+1);
                        o.setOverdueMoney(o.getOverdueMoney().add(money));
                        o.setWateMoney(o.getWateMoney().add(money));
                        o.setNeedPayMoney(o.getNeedPayMoney().add(money));
                        loanOrderService.updateById(o);
                        //发送逾期短信
                        EntityWrapper<MsgModel> ewMsg = new EntityWrapper();
                        ewMsg.eq("type",5);
                        MsgModel msgModel = msgModelService.selectOne(ewMsg);//找到短信模板
                        //将信息部分替换
                        User user = userService.selectById(o.getUserId());
                        String content = msgModel.getContent().replaceAll("</?[^>]+>", "").replace("#userName",user.getUserName()).replace("#borrowMoney",o.getBorrowMoney().toString()).replace("overDays",o.getOverdueDays().toString()).replace("#allowMoney",o.getAllowMoney().toString()).replace("#needPayMoney",o.getNeedPayMoney().toString());
                        SmsTempletEnum.REMINDSEND.setTypeName(content);
                        smsService.smsCodeByType(user.getPhone(), 3);
                   }else{
                        //容限期一天的费用
                        BigDecimal money = o.getBorrowMoney().multiply(new BigDecimal(paramSetting.getAllowPercent()*0.01)).setScale(1, BigDecimal.ROUND_HALF_UP);
                        o.setAllowMoney(o.getAllowMoney().add(money));
                        o.setAllowDays(o.getAllowDays()+1);
                        o.setOverdueDays(o.getOverdueDays()+1);
                        o.setOverdueMoney(o.getOverdueMoney().add(money));
                        o.setWateMoney(o.getWateMoney().add(money));
                        o.setNeedPayMoney(o.getNeedPayMoney().add(money));
                        loanOrderService.updateById(o);
                        //发送逾期短信
                        EntityWrapper<MsgModel> ewMsg = new EntityWrapper();
                        ewMsg.eq("type",5);
                        MsgModel msgModel = msgModelService.selectOne(ewMsg);//找到短信模板
                        //将信息部分替换
                        User user = userService.selectById(o.getUserId());
                        String content = msgModel.getContent().replaceAll("</?[^>]+>", "").replace("#userName",user.getUserName()).replace("#borrowMoney",o.getBorrowMoney().toString()).replace("overDays",o.getOverdueDays().toString()).replace("#allowMoney",o.getAllowMoney().toString()).replace("#needPayMoney",o.getNeedPayMoney().setScale(2,RoundingMode.HALF_UP).toString());
                        SmsTempletEnum.REMINDSEND.setTypeName(content);
                        smsService.smsCodeByType(user.getPhone(), 3);
                    }
               }
                //状态超出容限期时
                else if(o.getOrderStatus()==5){
                    //超出容限期一天的费用
                    if(o.getOverdueDays()<=60){
                        BigDecimal money = o.getBorrowMoney().multiply(new BigDecimal(paramSetting.getOverduePercent()*0.01)).setScale(1, BigDecimal.ROUND_HALF_UP);
                        o.setOverdueMoney(o.getOverdueMoney().add(money));
                        o.setWateMoney(o.getWateMoney().add(money));
                        o.setNeedPayMoney(o.getNeedPayMoney().add(money));
                    }
                    o.setOverdueDays(o.getOverdueDays()+1);
                    loanOrderService.updateById(o);
                    //发送逾期短信
                    EntityWrapper<MsgModel> ewMsg = new EntityWrapper();
                    ewMsg.eq("type",5);
                    MsgModel msgModel = msgModelService.selectOne(ewMsg);//找到短信模板
                    //将信息部分替换
                    User user = userService.selectById(o.getUserId());
                    String content = msgModel.getContent().replaceAll("</?[^>]+>", "").replace("#userName",user.getUserName()).replace("#borrowMoney",o.getBorrowMoney().toString()).replace("overDays",o.getOverdueDays().toString()).replace("#allowMoney",o.getAllowMoney().toString()).replace("#needPayMoney",o.getNeedPayMoney().setScale(2,RoundingMode.HALF_UP).toString());
                    SmsTempletEnum.REMINDSEND.setTypeName(content);
                    smsService.smsCodeByType(user.getPhone(), 3);
                }
            }
        }



    }
}
